namespace DotNetNuke.Entities.Portals
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Entities.Tabs;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Services.FileSystem;
    using DotNetNuke.UI.Skins;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Data;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Web;

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// PortalSettings Class
    /// 
    /// This class encapsulates all of the settings for the Portal, as well
    /// as the configuration settings required to execute the current tab
    /// view within the portal.
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [cnurse]	10/21/2004	documented
    /// [cnurse]	10/21/2004	added GetTabModuleSettings
    /// </history>
    /// -----------------------------------------------------------------------------
    public class PortalSettings
    {
        private DotNetNuke.Entities.Tabs.TabInfo _ActiveTab;
        private int _AdministratorId;
        private int _AdministratorRoleId;
        private string _AdministratorRoleName;
        private int _AdminTabId;
        private string _BackgroundFile;
        private int _BannerAdvertising;
        private string _Currency;
        private string _DefaultLanguage;
        private string _Description;
        private ArrayList _DesktopTabs;
        private string _Email;
        private DateTime _ExpiryDate;
        private string _FooterText;
        private string _HomeDirectory;
        private int _HomeTabId;
        private float _HostFee;
        private int _HostSpace;
        private string _KeyWords;
        private int _LoginTabId;
        private string _LogoFile;
        private PortalAliasInfo _PortalAlias;
        private int _PortalId;
        private string _PortalName;
        private int _RegisteredRoleId;
        private string _RegisteredRoleName;
        private int _SiteLogHistory;
        private int _SplashTabId;
        private int _SuperTabId;
        private int _TimeZoneOffset;
        private int _UserRegistration;
        private int _UserTabId;
        private string _Version;

        public PortalSettings()
        {
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The PortalSettings Constructor encapsulates all of the logic
        /// necessary to obtain configuration settings necessary to render
        /// a Portal Tab view for a given request.
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="tabId">The current tab</param>
        /// <param name="objPortalAliasInfo">The current portal</param>
        /// <history>
        /// [cnurse]	10/21/2004	documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public PortalSettings(int tabId, PortalAliasInfo objPortalAliasInfo)
        {
            this._DesktopTabs = new ArrayList();
            this._ActiveTab = new DotNetNuke.Entities.Tabs.TabInfo();
            this.GetPortalSettings(tabId, objPortalAliasInfo);
        }

        public static string ExecuteScript(string strScript)
        {
            return DataProvider.Instance().ExecuteScript(strScript);
        }

        public static string ExecuteScript(string strScript, bool UseTransactions)
        {
            return DataProvider.Instance().ExecuteScript(strScript, UseTransactions);
        }

        public static bool FindDatabaseVersion(int Major, int Minor, int Build)
        {
            bool FindDatabaseVersion = false;
            IDataReader dr = DataProvider.Instance().FindDatabaseVersion(Major, Minor, Build);
            if (dr.Read())
            {
                FindDatabaseVersion = true;
            }
            dr.Close();
            return FindDatabaseVersion;
        }

        private void GetBreadCrumbsRecursively(ref ArrayList objBreadCrumbs, int intTabId)
        {
            IEnumerator refObjectHelperL0=null;
            bool blnFound = false;
            DotNetNuke.Entities.Tabs.TabInfo objTab = null;
            try
            {
                refObjectHelperL0 = this.DesktopTabs.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    objTab = (DotNetNuke.Entities.Tabs.TabInfo) refObjectHelperL0.Current;
                    if (objTab.TabID == intTabId)
                    {
                        blnFound = true;
                        goto Label_005F;
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
        Label_005F:
            if (blnFound)
            {
                objBreadCrumbs.Insert(0, objTab.Clone());
                if (!Null.IsNull(objTab.ParentId))
                {
                    this.GetBreadCrumbsRecursively(ref objBreadCrumbs, objTab.ParentId);
                }
            }
        }

        public static IDataReader GetDatabaseVersion()
        {
            return DataProvider.Instance().GetDatabaseVersion();
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The GetHostSettings method returns a hashtable of
        /// host settings from the database.
        /// </summary>
        /// <returns>A Hashtable of settings (key/value pairs)</returns>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public static Hashtable GetHostSettings()
        {
            return DotNetNuke.Common.Globals.HostSettings;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The GetModuleSettings Method returns a hashtable of
        /// custom module specific settings from the database.  This method is
        /// used by some user control modules (Xml, Image, etc) to access misc
        /// settings.
        /// </summary>
        /// <returns>A Hashtable of settings (key/value pairs)</returns>
        /// <remarks>
        /// </remarks>
        /// <param name="ModuleId">The Module</param>
        /// <history>
        /// [cnurse]	10/21/2004	documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static Hashtable GetModuleSettings(int ModuleId)
        {
            Hashtable _settings = new Hashtable();
            IDataReader dr = DataProvider.Instance().GetModuleSettings(ModuleId);
            while (dr.Read())
            {
                if (!dr.IsDBNull(1))
                {
                    _settings[dr.GetString(0)] = dr.GetString(1);
                }
                else
                {
                    _settings[dr.GetString(0)] = "";
                }
            }
            dr.Close();
            return _settings;
        }

        public static PortalAliasInfo GetPortalAliasInfo(string PortalAlias)
        {
            string strPortalAlias;
            PortalAliasInfo objPortalAliasInfo = GetPortalAliasLookup()[PortalAlias.ToLower()];
            if (objPortalAliasInfo == null)
            {
                if (PortalAlias.ToLower().StartsWith("www."))
                {
                    strPortalAlias = PortalAlias.Replace("www.", "");
                }
                else
                {
                    strPortalAlias = "www." + PortalAlias;
                }
                objPortalAliasInfo = GetPortalAliasLookup()[strPortalAlias.ToLower()];
            }
            if (objPortalAliasInfo == null)
            {
                if (PortalAlias.IndexOf(".") != -1)
                {
                    strPortalAlias = PortalAlias.Substring(PortalAlias.IndexOf(".") + 1);
                }
                else
                {
                    strPortalAlias = PortalAlias;
                }
                if (objPortalAliasInfo == null)
                {
                    objPortalAliasInfo = GetPortalAliasLookup()["*." + strPortalAlias.ToLower()];
                }
                if (objPortalAliasInfo == null)
                {
                    objPortalAliasInfo = GetPortalAliasLookup()[strPortalAlias.ToLower()];
                }
                if (objPortalAliasInfo == null)
                {
                    objPortalAliasInfo = GetPortalAliasLookup()["www." + strPortalAlias.ToLower()];
                }
            }
            if (objPortalAliasInfo == null)
            {
                PortalAliasCollection objPortalAliasCollection = GetPortalAliasLookup();
                if (((!objPortalAliasCollection.HasKeys || ((objPortalAliasCollection.Count == 1) & objPortalAliasCollection.Contains("_default"))) ? 1 : 0) != 0)
                {
                    DataProvider.Instance().UpdatePortalAlias(PortalAlias.ToLower());
                    DataCache.RemoveCache("GetPortalByAlias");
                    objPortalAliasInfo = GetPortalAliasLookup()[PortalAlias.ToLower()];
                }
            }
            return objPortalAliasInfo;
        }

        public static PortalAliasCollection GetPortalAliasLookup()
        {
            PortalAliasCollection objPortalAliasCollection = (PortalAliasCollection) DataCache.GetCache("GetPortalByAlias");
            try
            {
                if (objPortalAliasCollection == null)
                {
                    objPortalAliasCollection = new PortalAliasController().GetPortalAliases();
                    DataCache.SetCache("GetPortalByAlias", objPortalAliasCollection);
                }
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                HttpContext objHttpContext = HttpContext.Current;
                StreamReader objStreamReader = File.OpenText(objHttpContext.Server.MapPath("~/500.htm"));
                string strHTML = objStreamReader.ReadToEnd();
                objStreamReader.Close();
                strHTML = Strings.Replace(strHTML, "[MESSAGE]", "ERROR: Could not connect to database.<br><br>" + exc.Message, 1, -1, CompareMethod.Binary);
                objHttpContext.Response.Write(strHTML);
                objHttpContext.Response.End();
                
            }
            return objPortalAliasCollection;
        }

        public static string GetPortalByID(int PortalId, string PortalAlias)
        {
            string retValue = "";
            PortalAliasCollection objPortalAliasCollection = GetPortalAliasLookup();
            bool bFound = false;
            PortalAliasInfo objPortalAliasInfo = objPortalAliasCollection[PortalAlias.ToLower()];
            if ((objPortalAliasInfo != null) && (objPortalAliasInfo.PortalID == PortalId))
            {
                retValue = objPortalAliasInfo.HTTPAlias;
                bFound = true;
            }
            if (!bFound)
            {
                IEnumerator refObjectHelperL0=null;
                try
                {
                    refObjectHelperL0 = objPortalAliasCollection.Keys.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        string key = Conversions.ToString(refObjectHelperL0.Current);
                        objPortalAliasInfo = objPortalAliasCollection[key];
                        string strHTTPAlias = objPortalAliasInfo.HTTPAlias.ToLower();
                        if (strHTTPAlias.StartsWith(PortalAlias.ToLower()) & (objPortalAliasInfo.PortalID == PortalId))
                        {
                            return objPortalAliasInfo.HTTPAlias;
                        }
                        if (strHTTPAlias.StartsWith("www."))
                        {
                            strHTTPAlias = strHTTPAlias.Replace("www.", "");
                        }
                        else
                        {
                            strHTTPAlias = "www." + strHTTPAlias;
                        }
                        if (strHTTPAlias.StartsWith(PortalAlias.ToLower()) & (objPortalAliasInfo.PortalID == PortalId))
                        {
                            return objPortalAliasInfo.HTTPAlias;
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
            }
            return retValue;
        }

        public static string GetPortalByTab(int TabID, string PortalAlias)
        {
            int intPortalId = -2;
            DotNetNuke.Entities.Tabs.TabInfo objTab = new TabController().GetTab(TabID);
            if ((objTab != null) && !objTab.IsDeleted)
            {
                intPortalId = objTab.PortalID;
            }
            string GetPortalByTab = null;
            switch (intPortalId)
            {
                case -2:
                    return GetPortalByTab;

                case -1:
                    return PortalAlias;
            }
            return GetPortalByID(intPortalId, PortalAlias);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The GetPortalSettings method builds the site Settings
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="TabId">The current tabs id</param>
        /// <param name="objPortalAliasInfo">The Portal Alias object</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        private void GetPortalSettings(int TabId, PortalAliasInfo objPortalAliasInfo)
        {
            ModuleInfo objModule=null;
            DotNetNuke.Entities.Tabs.TabInfo objTab=null;
            IEnumerator refObjectHelperL2=null;
            IEnumerator refObjectHelperL7=null;
            PortalController objPortals = new PortalController();
            TabController objTabs = new TabController();
            ModuleController objModules = new ModuleController();
            int intCacheTimeout = 20 * Convert.ToInt32((int) DotNetNuke.Common.Globals.PerformanceSetting);
            this.PortalId = objPortalAliasInfo.PortalID;
            PortalInfo objPortal = (PortalInfo) DataCache.GetPersistentCacheItem("GetPortalSettings" + this.PortalId.ToString(), typeof(PortalInfo));
            if (objPortal == null)
            {
                objPortal = objPortals.GetPortal(this.PortalId);
                if (objPortal != null)
                {
                    if (Null.IsNull(objPortal.HostSpace))
                    {
                        objPortal.HostSpace = 0;
                    }
                    if (Null.IsNull(objPortal.DefaultLanguage))
                    {
                        objPortal.DefaultLanguage = "en-US";
                    }
                    if (Null.IsNull(objPortal.TimeZoneOffset))
                    {
                        objPortal.TimeZoneOffset = -480;
                    }
                    objPortal.HomeDirectory = DotNetNuke.Common.Globals.ApplicationPath + "/" + objPortal.HomeDirectory + "/";
                    Array arrVersion = "04.00.00".Split(new char[] { '.' });
                    int intMajor = Conversions.ToInteger(arrVersion.GetValue(0));
                    int intMinor = Conversions.ToInteger(arrVersion.GetValue(1));
                    int intBuild = Conversions.ToInteger(arrVersion.GetValue(2));
                    objPortal.Version = intMajor.ToString() + "." + intMinor.ToString() + "." + intBuild.ToString();
                    UserInfo objUser = new UserController().GetUser(objPortal.PortalID, objPortal.AdministratorId);
                    if (objUser != null)
                    {
                        objPortal.Email = objUser.Membership.Email;
                    }
                    if (intCacheTimeout != 0)
                    {
                        DataCache.SetCache("GetPortalSettings" + this.PortalId.ToString(), objPortal, TimeSpan.FromMinutes((double) intCacheTimeout), true);
                    }
                }
            }
            if (objPortal != null)
            {
                this.PortalAlias = objPortalAliasInfo;
                this.PortalId = objPortal.PortalID;
                this.PortalName = objPortal.PortalName;
                this.LogoFile = objPortal.LogoFile;
                this.FooterText = objPortal.FooterText;
                this.ExpiryDate = objPortal.ExpiryDate;
                this.UserRegistration = objPortal.UserRegistration;
                this.BannerAdvertising = objPortal.BannerAdvertising;
                this.Currency = objPortal.Currency;
                this.AdministratorId = objPortal.AdministratorId;
                this.Email = objPortal.Email;
                this.HostFee = objPortal.HostFee;
                this.HostSpace = objPortal.HostSpace;
                this.AdministratorRoleId = objPortal.AdministratorRoleId;
                this.AdministratorRoleName = objPortal.AdministratorRoleName;
                this.RegisteredRoleId = objPortal.RegisteredRoleId;
                this.RegisteredRoleName = objPortal.RegisteredRoleName;
                this.Description = objPortal.Description;
                this.KeyWords = objPortal.KeyWords;
                this.BackgroundFile = objPortal.BackgroundFile;
                this.SiteLogHistory = objPortal.SiteLogHistory;
                this.AdminTabId = objPortal.AdminTabId;
                this.SuperTabId = objPortal.SuperTabId;
                this.SplashTabId = objPortal.SplashTabId;
                this.HomeTabId = objPortal.HomeTabId;
                this.LoginTabId = objPortal.LoginTabId;
                this.UserTabId = objPortal.UserTabId;
                this.DefaultLanguage = objPortal.DefaultLanguage;
                this.TimeZoneOffset = objPortal.TimeZoneOffset;
                this.HomeDirectory = objPortal.HomeDirectory;
                this.Version = objPortal.Version;
            }
            ArrayList arrTabs = (ArrayList) DataCache.GetCache("GetTabs" + this.PortalId.ToString());
            if (arrTabs == null)
            {
                arrTabs = objTabs.GetTabs(this.PortalId);
                if (arrTabs != null)
                {
                    IEnumerator refObjectHelperL0=null;
                    try
                    {
                        refObjectHelperL0 = arrTabs.GetEnumerator();
                        while (refObjectHelperL0.MoveNext())
                        {
                            objTab = (DotNetNuke.Entities.Tabs.TabInfo) refObjectHelperL0.Current;
                            if (objTab.TabOrder == 0)
                            {
                                objTab.TabOrder = 0x3e7;
                            }
                            if (Null.IsNull(objTab.StartDate))
                            {
                                objTab.StartDate = DateTime.MinValue;
                            }
                            if (Null.IsNull(objTab.EndDate))
                            {
                                objTab.EndDate = DateTime.MaxValue;
                            }
                            objTab.IsSuperTab = false;
                        }
                    }
                    finally
                    {
                        if (refObjectHelperL0 is IDisposable)
                        {
                            (refObjectHelperL0 as IDisposable).Dispose();
                        }
                    }
                    objTab = objTabs.GetTab(this.SuperTabId);
                    if (objTab != null)
                    {
                        objTab.PortalID = this.PortalId;
                        objTab.StartDate = DateTime.MinValue;
                        objTab.EndDate = DateTime.MaxValue;
                        objTab.IsSuperTab = true;
                        arrTabs.Add(objTab);
                    }
                    ArrayList arrHostTabs = objTabs.GetTabsByParentId(this.SuperTabId);
                    if (arrHostTabs != null)
                    {
                        IEnumerator refObjectHelperL1=null;
                        try
                        {
                            refObjectHelperL1 = arrHostTabs.GetEnumerator();
                            while (refObjectHelperL1.MoveNext())
                            {
                                objTab = (DotNetNuke.Entities.Tabs.TabInfo) refObjectHelperL1.Current;
                                objTab.PortalID = this.PortalId;
                                objTab.StartDate = DateTime.MinValue;
                                objTab.EndDate = DateTime.MaxValue;
                                objTab.IsSuperTab = true;
                                arrTabs.Add(objTab);
                            }
                        }
                        finally
                        {
                            if (refObjectHelperL1 is IDisposable)
                            {
                                (refObjectHelperL1 as IDisposable).Dispose();
                            }
                        }
                    }
                    if (intCacheTimeout != 0)
                    {
                        DataCache.SetCache("GetTabs" + this.PortalId.ToString(), arrTabs, TimeSpan.FromMinutes((double) intCacheTimeout));
                    }
                }
            }
            try
            {
                refObjectHelperL2 = arrTabs.GetEnumerator();
                while (refObjectHelperL2.MoveNext())
                {
                    objTab = (DotNetNuke.Entities.Tabs.TabInfo) refObjectHelperL2.Current;
                    this.DesktopTabs.Add(objTab.Clone());
                }
            }
            finally
            {
                if (refObjectHelperL2 is IDisposable)
                {
                    (refObjectHelperL2 as IDisposable).Dispose();
                }
            }
            int intTabId = this.VerifyPortalTab(this.PortalId, TabId);
            objTab = (DotNetNuke.Entities.Tabs.TabInfo) DataCache.GetCache("GetTab" + intTabId.ToString());
            if (objTab == null)
            {
                SkinInfo objSkin;
                objTab = objTabs.GetTab(intTabId);
                if (objTab == null)
                {
                    return;
                }
                if (Null.IsNull(objTab.StartDate))
                {
                    objTab.StartDate = DateTime.MinValue;
                }
                if (Null.IsNull(objTab.EndDate))
                {
                    objTab.EndDate = DateTime.MaxValue;
                }
                if (objTab.SkinSrc == "")
                {
                    if (DotNetNuke.Common.Globals.IsAdminSkin(objTab.IsAdminTab))
                    {
                        objSkin = SkinController.GetSkin(SkinInfo.RootSkin, this.PortalId, SkinType.Admin);
                    }
                    else
                    {
                        objSkin = SkinController.GetSkin(SkinInfo.RootSkin, this.PortalId, SkinType.Portal);
                    }
                    if (objSkin != null)
                    {
                        objTab.SkinSrc = objSkin.SkinSrc;
                    }
                }
                if (objTab.SkinSrc == "")
                {
                    if (DotNetNuke.Common.Globals.IsAdminSkin(objTab.IsAdminTab))
                    {
                        objTab.SkinSrc = "[G]" + SkinInfo.RootSkin + "/DNN-Blue/Horizontal Menu - Fixed Width.ascx";
                    }
                    else
                    {
                        objTab.SkinSrc = "[G]" + SkinInfo.RootSkin + "/DNN-Blue/Horizontal Menu - Fixed Width.ascx";
                    }
                }
                objTab.SkinSrc = SkinController.FormatSkinSrc(objTab.SkinSrc, this);
                objTab.SkinPath = SkinController.FormatSkinPath(objTab.SkinSrc);
                if (objTab.ContainerSrc == "")
                {
                    if (DotNetNuke.Common.Globals.IsAdminSkin(objTab.IsAdminTab))
                    {
                        objSkin = SkinController.GetSkin(SkinInfo.RootContainer, this.PortalId, SkinType.Admin);
                    }
                    else
                    {
                        objSkin = SkinController.GetSkin(SkinInfo.RootContainer, this.PortalId, SkinType.Portal);
                    }
                    if (objSkin != null)
                    {
                        objTab.ContainerSrc = objSkin.SkinSrc;
                    }
                }
                if (objTab.ContainerSrc == "")
                {
                    if (DotNetNuke.Common.Globals.IsAdminSkin(objTab.IsAdminTab))
                    {
                        objTab.ContainerSrc = "[G]" + SkinInfo.RootContainer + "/DNN-Blue/Image Header - Color Background.ascx";
                    }
                    else
                    {
                        objTab.ContainerSrc = "[G]" + SkinInfo.RootContainer + "/DNN-Blue/Image Header - Color Background.ascx";
                    }
                }
                objTab.ContainerSrc = SkinController.FormatSkinSrc(objTab.ContainerSrc, this);
                objTab.ContainerPath = SkinController.FormatSkinPath(objTab.ContainerSrc);
                objTab.BreadCrumbs = new ArrayList();
                objTab.Panes = new ArrayList();
                objTab.Modules = new ArrayList();
                if (objTab.ParentId == this.SuperTabId)
                {
                    objTab.IsSuperTab = true;
                }
                DotNetNuke.Entities.Tabs.TabInfo refObjectHelperS3 = objTab;
                ArrayList refObjectHelperS4 = refObjectHelperS3.BreadCrumbs;
                this.GetBreadCrumbsRecursively(ref refObjectHelperS4, intTabId);
                refObjectHelperS3.BreadCrumbs = refObjectHelperS4;
                if (intCacheTimeout != 0)
                {
                    DataCache.SetCache("GetTab" + intTabId.ToString(), objTab, TimeSpan.FromMinutes((double) intCacheTimeout));
                }
            }
            if (objTab != null)
            {
                this.ActiveTab = objTab.Clone();
            }
            Hashtable objPaneModules = new Hashtable();
            ArrayList arrModules = (ArrayList) DataCache.GetCache("GetPortalTabModules" + intTabId.ToString());
            if (arrModules == null)
            {
                arrModules = objModules.GetPortalTabModules(this.PortalId, this.ActiveTab.TabID);
                if (arrModules != null)
                {
                    IEnumerator refObjectHelperL5=null;
                    IEnumerator refObjectHelperL6 = null;
                    try
                    {
                        refObjectHelperL5 = arrModules.GetEnumerator();
                        while (refObjectHelperL5.MoveNext())
                        {
                            objModule = (ModuleInfo) refObjectHelperL5.Current;
                            if (Null.IsNull(objModule.StartDate))
                            {
                                objModule.StartDate = DateTime.MinValue;
                            }
                            if (Null.IsNull(objModule.EndDate))
                            {
                                objModule.EndDate = DateTime.MaxValue;
                            }
                            if (objModule.ContainerSrc == "")
                            {
                                objModule.ContainerSrc = this.ActiveTab.ContainerSrc;
                            }
                            objModule.ContainerSrc = SkinController.FormatSkinSrc(objModule.ContainerSrc, this);
                            objModule.ContainerPath = SkinController.FormatSkinPath(objModule.ContainerSrc);
                            if (!objPaneModules.ContainsKey(objModule.PaneName))
                            {
                                objPaneModules.Add(objModule.PaneName, 0);
                            }
                            objModule.PaneModuleCount = 0;
                            if (!objModule.IsDeleted)
                            {
                                objPaneModules[objModule.PaneName] = Conversions.ToInteger(objPaneModules[objModule.PaneName]) + 1;
                                objModule.PaneModuleIndex = Conversions.ToInteger(objPaneModules[objModule.PaneName]) - 1;
                            }
                        }
                    }
                    finally
                    {
                        if (refObjectHelperL5 is IDisposable)
                        {
                            (refObjectHelperL5 as IDisposable).Dispose();
                        }
                    }
                    try
                    {
                        refObjectHelperL6 = arrModules.GetEnumerator();
                        while (refObjectHelperL6.MoveNext())
                        {
                            objModule = (ModuleInfo) refObjectHelperL6.Current;
                            objModule.PaneModuleCount = Convert.ToInt32(RuntimeHelpers.GetObjectValue(objPaneModules[objModule.PaneName]));
                        }
                    }
                    finally
                    {
                        if (refObjectHelperL6 is IDisposable)
                        {
                            (refObjectHelperL6 as IDisposable).Dispose();
                        }
                    }
                    if (intCacheTimeout != 0)
                    {
                        DataCache.SetCache("GetPortalTabModules" + intTabId.ToString(), arrModules, TimeSpan.FromMinutes((double) intCacheTimeout));
                    }
                }
            }
            try
            {
                refObjectHelperL7 = arrModules.GetEnumerator();
                while (refObjectHelperL7.MoveNext())
                {
                    objModule = (ModuleInfo) refObjectHelperL7.Current;
                    this.ActiveTab.Modules.Add(objModule.Clone());
                }
            }
            finally
            {
                if (refObjectHelperL7 is IDisposable)
                {
                    (refObjectHelperL7 as IDisposable).Dispose();
                }
            }
        }

        public static string GetProviderPath()
        {
            return DataProvider.Instance().GetProviderPath();
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The GetSiteSettings method returns a hashtable of
        /// portal specific settings from the database.  This method 
        /// uses the Site Settings module as a convenient storage area for
        /// portal-wide settings.
        /// </summary>
        /// <returns>A Hashtable of settings (key/value pairs)</returns>
        /// <remarks>
        /// </remarks>
        /// <param name="PortalId">The Portal</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public static Hashtable GetSiteSettings(int PortalId)
        {
            ModuleController objModules = new ModuleController();
            return GetModuleSettings(objModules.GetModuleByDefinition(PortalId, "Site Settings").ModuleID);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The GetTabModuleSettings Method returns a hashtable of
        /// custom module/tab specific settings from the database.  This method is
        /// used by some user control modules (Xml, Image, etc) to access misc
        /// settings.
        /// </summary>
        /// <returns>A Hashtable of settings (key/value pairs)</returns>
        /// <remarks>
        /// </remarks>
        /// <param name="TabModuleId">The current tabModule</param>
        /// <history>
        /// [cnurse]	10/21/2004	documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static Hashtable GetTabModuleSettings(int TabModuleId)
        {
            ModuleController objModules = new ModuleController();
            return objModules.GetTabModuleSettings(TabModuleId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The GetTabModuleSettings Method returns a hashtable of
        /// custom module/tab specific settings from the database.  This method is
        /// used by some user control modules (Xml, Image, etc) to access misc
        /// settings.
        /// </summary>
        /// <returns>A Hashtable of settings (key/value pairs)</returns>
        /// <remarks>
        /// </remarks>
        /// <param name="TabModuleId">The current tabmodule</param>
        /// <param name="settings">A Hashtable to add the Settings to</param>
        /// <history>
        /// [cnurse]	10/21/2004	documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static Hashtable GetTabModuleSettings(int TabModuleId, Hashtable settings)
        {
            IDataReader dr = DataProvider.Instance().GetTabModuleSettings(TabModuleId);
            while (dr.Read())
            {
                if (!dr.IsDBNull(1))
                {
                    settings[dr.GetString(0)] = dr.GetString(1);
                }
                else
                {
                    settings[dr.GetString(0)] = "";
                }
            }
            dr.Close();
            return settings;
        }

        public static void UpdateDatabaseVersion(int Major, int Minor, int Build)
        {
            DataProvider.Instance().UpdateDatabaseVersion(Major, Minor, Build);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The UpdatePortalSetting method updates a specific portal setting
        /// in the database. Since this is a portal-wide storage area you must
        /// be careful to avoid naming collisions on SettingNames.
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="PortalId">The Portal</param>
        /// <param name="SettingName">The Setting Name</param>
        /// <param name="SettingValue">The Setting Value</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void UpdatePortalSetting(int PortalId, string SettingName, string SettingValue)
        {
            ModuleController objModules = new ModuleController();
            int ModuleId = objModules.GetModuleByDefinition(PortalId, "Site Settings").ModuleID;
            objModules.UpdateModuleSetting(ModuleId, SettingName, SettingValue);
        }

        public static void UpgradeDatabaseSchema(int Major, int Minor, int Build)
        {
            DataProvider.Instance().UpgradeDatabaseSchema(Major, Minor, Build);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The VerifyPortalTab method verifies that the TabId/PortalId combination
        /// is allowed and returns default/home tab ids if not
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        /// </remarks>
        /// <param name="PortalId">The Portal's id</param>
        /// <param name="TabId">The current tab's id</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        private int VerifyPortalTab(int PortalId, int TabId)
        {
            DotNetNuke.Entities.Tabs.TabInfo objTab;
            int VerifyPortalTab = -1;
            if (TabId != -1)
            {
                IEnumerator refObjectHelperL0=null;
                try
                {
                    refObjectHelperL0 = this.DesktopTabs.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        objTab = (DotNetNuke.Entities.Tabs.TabInfo) refObjectHelperL0.Current;
                        if ((objTab.TabID == TabId) && !objTab.IsDeleted)
                        {
                            VerifyPortalTab = objTab.TabID;
                            goto Label_0085;
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
            }
        Label_0085:
            if ((VerifyPortalTab == -1) & (this.SplashTabId > 0))
            {
                VerifyPortalTab = this.SplashTabId;
            }
            if ((VerifyPortalTab == -1) & (this.HomeTabId > 0))
            {
                VerifyPortalTab = this.HomeTabId;
            }
            if (VerifyPortalTab == -1)
            {
                int refIntHelperL0 = this.DesktopTabs.Count;
                for (int i = 0; i <= refIntHelperL0; i++)
                {
                    objTab = (DotNetNuke.Entities.Tabs.TabInfo) this.DesktopTabs[i];
                    if (!objTab.IsDeleted & objTab.IsVisible)
                    {
                        return objTab.TabID;
                    }
                }
            }
            return VerifyPortalTab;
        }

        public DotNetNuke.Entities.Tabs.TabInfo ActiveTab
        {
            get
            {
                return this._ActiveTab;
            }
            set
            {
                this._ActiveTab = value;
            }
        }

        public int AdministratorId
        {
            get
            {
                return this._AdministratorId;
            }
            set
            {
                this._AdministratorId = value;
            }
        }

        public int AdministratorRoleId
        {
            get
            {
                return this._AdministratorRoleId;
            }
            set
            {
                this._AdministratorRoleId = value;
            }
        }

        public string AdministratorRoleName
        {
            get
            {
                return this._AdministratorRoleName;
            }
            set
            {
                this._AdministratorRoleName = value;
            }
        }

        public int AdminTabId
        {
            get
            {
                return this._AdminTabId;
            }
            set
            {
                this._AdminTabId = value;
            }
        }

        public string BackgroundFile
        {
            get
            {
                return this._BackgroundFile;
            }
            set
            {
                this._BackgroundFile = value;
            }
        }

        public int BannerAdvertising
        {
            get
            {
                return this._BannerAdvertising;
            }
            set
            {
                this._BannerAdvertising = value;
            }
        }

        public string Currency
        {
            get
            {
                return this._Currency;
            }
            set
            {
                this._Currency = value;
            }
        }

        public string DefaultLanguage
        {
            get
            {
                return this._DefaultLanguage;
            }
            set
            {
                this._DefaultLanguage = value;
            }
        }

        public string Description
        {
            get
            {
                return this._Description;
            }
            set
            {
                this._Description = value;
            }
        }

        public ArrayList DesktopTabs
        {
            get
            {
                return this._DesktopTabs;
            }
            set
            {
                this._DesktopTabs = value;
            }
        }

        public string Email
        {
            get
            {
                return this._Email;
            }
            set
            {
                this._Email = value;
            }
        }

        public DateTime ExpiryDate
        {
            get
            {
                return this._ExpiryDate;
            }
            set
            {
                this._ExpiryDate = value;
            }
        }

        public string FooterText
        {
            get
            {
                return this._FooterText;
            }
            set
            {
                this._FooterText = value;
            }
        }

        public string HomeDirectory
        {
            get
            {
                return this._HomeDirectory;
            }
            set
            {
                this._HomeDirectory = value;
            }
        }

        public string HomeDirectoryMapPath
        {
            get
            {
                FolderController objFolderController = new FolderController();
                return objFolderController.GetMappedDirectory(this.HomeDirectory);
            }
        }

        public int HomeTabId
        {
            get
            {
                return this._HomeTabId;
            }
            set
            {
                this._HomeTabId = value;
            }
        }

        public float HostFee
        {
            get
            {
                return this._HostFee;
            }
            set
            {
                this._HostFee = value;
            }
        }

        public Hashtable HostSettings
        {
            get
            {
                return DotNetNuke.Common.Globals.HostSettings;
            }
        }

        public int HostSpace
        {
            get
            {
                return this._HostSpace;
            }
            set
            {
                this._HostSpace = value;
            }
        }

        public string KeyWords
        {
            get
            {
                return this._KeyWords;
            }
            set
            {
                this._KeyWords = value;
            }
        }

        public int LoginTabId
        {
            get
            {
                return this._LoginTabId;
            }
            set
            {
                this._LoginTabId = value;
            }
        }

        public string LogoFile
        {
            get
            {
                return this._LogoFile;
            }
            set
            {
                this._LogoFile = value;
            }
        }

        public PortalAliasInfo PortalAlias
        {
            get
            {
                return this._PortalAlias;
            }
            set
            {
                this._PortalAlias = value;
            }
        }

        public int PortalId
        {
            get
            {
                return this._PortalId;
            }
            set
            {
                this._PortalId = value;
            }
        }

        public string PortalName
        {
            get
            {
                return this._PortalName;
            }
            set
            {
                this._PortalName = value;
            }
        }

        public int RegisteredRoleId
        {
            get
            {
                return this._RegisteredRoleId;
            }
            set
            {
                this._RegisteredRoleId = value;
            }
        }

        public string RegisteredRoleName
        {
            get
            {
                return this._RegisteredRoleName;
            }
            set
            {
                this._RegisteredRoleName = value;
            }
        }

        public int SiteLogHistory
        {
            get
            {
                return this._SiteLogHistory;
            }
            set
            {
                this._SiteLogHistory = value;
            }
        }

        public int SplashTabId
        {
            get
            {
                return this._SplashTabId;
            }
            set
            {
                this._SplashTabId = value;
            }
        }

        public int SuperTabId
        {
            get
            {
                return this._SuperTabId;
            }
            set
            {
                this._SuperTabId = value;
            }
        }

        public int TimeZoneOffset
        {
            get
            {
                return this._TimeZoneOffset;
            }
            set
            {
                this._TimeZoneOffset = value;
            }
        }

        public int UserRegistration
        {
            get
            {
                return this._UserRegistration;
            }
            set
            {
                this._UserRegistration = value;
            }
        }

        public int UserTabId
        {
            get
            {
                return this._UserTabId;
            }
            set
            {
                this._UserTabId = value;
            }
        }

        public string Version
        {
            get
            {
                return this._Version;
            }
            set
            {
                this._Version = value;
            }
        }
    }
}

